<template>
  <j-modal
    :title="title"
    :width="500"
    :visible="visible"
    :maskClosable="false"
    @close="close"
    cancelText="关闭">
    <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" ref="form" :rules="validatorRules" :model="model">
      <a-form-model-item label="文书名称" prop="name">
        <a-input v-model="model.name"></a-input>
      </a-form-model-item>
      <a-form-model-item label="文书类型" prop="type">
        <j-dict-select-tag
          style="width: 360px"
          v-model="model.type"
          dict-code="word_type"
        />
      </a-form-model-item>
      <a-form-model-item label="文书描述" prop="remarks">
        <a-input v-model="model.remarks"></a-input>
      </a-form-model-item>
      <a-form-model-item label="附件上传">
        <MBWHUpload ref="fileUpload" :fileData="fileData" @uploadFile="uploadFile"></MBWHUpload>
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
  import {httpAction, getAction} from '@/api/manage'
  import MBWHUpload from '../../components/MBWHUpload'

  export default {
    name: 'CheckAndEditModel',
    components: {
      MBWHUpload
    },
    data() {
      return {
        labelCol: {span: 4},
        wrapperCol: {span: 14},
        dataSource: [],
        title: '',
        visible: false,
        isCheck: false,
        model: {},
        fileData: {},
        validatorRules: {
          name: [{required: true, message: '请输入'}],
          type: [{required: true, message: '请选择'}],
        },
        url: {
          add: '/xtsz/sjWordTemplate/add',
          edit: '/xtsz/sjWordTemplate/edit'
        },
      }
    },
    created() {
    },
    methods: {

      add() {
        this.model = {}
        this.fileData = {}
        this.visible = true
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.fileData = Object.assign({}, record)

        this.visible = true
      },
      close(isSubmit) {
        if (isSubmit) {
          this.saveData()
        } else {
          this.visible = false
        }
      },
      saveData() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.$refs.fileUpload.saveFile()
          } else {
            return false
          }
        })
      },
      uploadFile(result) {
        let url, type
        if (!this.model.id) {
          url = this.url.add
          type = 'post'
        } else {
          url = this.url.edit
          type = 'put'
        }
        if (result) {
          this.model.fileName = result.fileName
          this.model.tempUrl = result.tempUrl
        } else {
          this.model.fileName = ''
          this.model.tempUrl = ''
        }

        httpAction(url, this.model, type).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.$emit('ok');
            this.visible = false
          } else {
            console.log(res)
          }
        })
      },
    }
  }
</script>

<style lang="less" scoped>

  .ant-input {
    width: 360px;
  }
</style>
